//
//  DebugMainWindow.m
//  YK_DebugManager
//
//  Created by edward on 2021/1/11.
//

#import "DebugMainWindow.h"
#import "DebugMainViewController.h"

@interface DebugMainWindow ()
///
@property (nonatomic, strong, readwrite) DebugMainViewController *mainVC;
@end

@implementation DebugMainWindow

static DebugMainWindow *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[DebugMainWindow alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    }
    return _instance;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        self.windowLevel = UIWindowLevelStatusBar - 1.0f;
        self.hidden = YES;
        #if defined(__IPHONE_13_0) && (__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0)
            if (@available(iOS 13.0, *)) {
                UIScene *scene = [[UIApplication sharedApplication].connectedScenes anyObject];
                if (scene) {
                    self.windowScene = (UIWindowScene *)scene;
                }
            }
        #endif
    }
    return self;
}

- (void)show
{
    [self setRoot:self.mainVC];
    self.hidden = NO;
}

- (void)dissMiss
{
    if(self.rootViewController.presentedViewController)
    {
        [self.rootViewController.presentedViewController dismissViewControllerAnimated:NO completion:nil];
    }
    [self setRoot:nil];
    self.hidden = YES;
}
- (void)setRoot:(UIViewController *)vc
{
    if(vc)
    {
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];
        
        UIColor *backgroundColor = UIColor.whiteColor;
        
        if (@available(iOS 15, *)) {
            
            
            UINavigationBarAppearance *standardAppearance = nav.navigationBar.standardAppearance;
            
            if (!standardAppearance) {
                standardAppearance = [[UINavigationBarAppearance alloc] init];
            }
            standardAppearance.backgroundColor = backgroundColor;
            nav.navigationBar.standardAppearance = standardAppearance;
            
            UINavigationBarAppearance *scrollEdgeAppearance = nav.navigationBar.scrollEdgeAppearance;
            
            if (!scrollEdgeAppearance) {
                scrollEdgeAppearance = [[UINavigationBarAppearance alloc] init];
            }
            
            scrollEdgeAppearance.backgroundColor = backgroundColor;
            nav.navigationBar.scrollEdgeAppearance = scrollEdgeAppearance;
            
        } else {
            nav.navigationBar.barTintColor = backgroundColor;
            nav.navigationBar.backgroundColor = backgroundColor;
        }
        
        
        NSDictionary *attributesDic = @{
                                        NSForegroundColorAttributeName:[UIColor blackColor],
                                        NSFontAttributeName:[UIFont systemFontOfSize:18]
                                        };
        [nav.navigationBar setTitleTextAttributes:attributesDic];
        _nav = nav;
        self.rootViewController = nav;
    }else
    {
        self.rootViewController = nil;
        _nav = nil;
    }
}

- (DebugMainViewController *)mainVC
{
    if (!_mainVC) {
        
        DebugMainViewController *debugVC = [[DebugMainViewController alloc]init];
        __weak typeof(self) weakself = self;
        debugVC.showOrDissMiss = ^{
            __strong typeof(weakself) strongself = weakself;
            if (strongself.isHidden) {
                [strongself show];
            }else
            {
                [strongself dissMiss];
            }
        };
        _mainVC = debugVC;
    }
    return _mainVC;
}

@end
